<template>
  <a-spin :spinning="confirmLoading">
    <a-card :bordered="false" class="card">
      <a-row type="flex" style="margin-top: 10px">
        <a-col :span="10">
          <a-card :bordered="false" :body-style="{ padding: '0px' }">
            <chapter-list
              ref="chapterRef"
              v-if="standardType == '综合指标' || standardType == '技术指标'"
              @change="chapterChange"
              :projectId="scoreStandardInfo.projectId"
              :scoreStandardId="scoreStandardInfo.id"
              :standardType="standardType"
            />
          </a-card>
        </a-col>
        <a-col :span="14">
          <a-card :bordered="false" :body-style="{ padding: '0px' }">
            <a-row>
              <!-- <a-button style="margin-left: 8px" type="primary" @click="validate">校验</a-button>
                <a-button style="margin-left: 8px" type="primary" @click="save">保存</a-button> -->
              <a-button style="float: right" type="primary" icon="save" @click="handleSave">保存</a-button>
            </a-row>
            <a-row style="margin-top: 10px; height: 800px">
              <a-spin :spinning="confirmLoading">
                <j-form-container>
                  <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
                    <a-row>
                      <a-col :span="24">
                        <a-form-model-item
                          label="指标名称"
                          :labelCol="labelCol"
                          :wrapperCol="wrapperCol"
                          prop="indicatorName"
                        >
                          <a-input v-model="model.indicatorName" placeholder="请输入指标名称"></a-input>
                        </a-form-model-item>
                      </a-col>
                      <a-col :span="24">
                        <a-form-model-item
                          label="指标类型"
                          :labelCol="labelCol"
                          :wrapperCol="wrapperCol"
                          prop="indicatorType"
                        >
                          <j-dict-select-tag
                            type="list"
                            v-model="model.indicatorType"
                            dictCode="indicator_type"
                            placeholder="请选择指标类型"
                          />
                        </a-form-model-item>
                      </a-col>
                      <a-col :span="24">
                        <a-form-model-item label="分值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="score">
                          <a-input-number v-model="model.score" placeholder="请输入分值" style="width: 100%" />
                        </a-form-model-item>
                      </a-col>
                      <a-col :span="24">
                        <a-form-model-item
                          label="最小分值"
                          :labelCol="labelCol"
                          :wrapperCol="wrapperCol"
                          prop="scoreMin"
                        >
                          <a-input-number v-model="model.scoreMin" placeholder="请输入最小分值" style="width: 100%" />
                        </a-form-model-item>
                      </a-col>
                      <a-col :span="24">
                        <a-form-model-item
                          label="最大分值"
                          :labelCol="labelCol"
                          :wrapperCol="wrapperCol"
                          prop="scoreMax"
                        >
                          <a-input-number v-model="model.scoreMax" placeholder="请输入最大分值" style="width: 100%" />
                        </a-form-model-item>
                      </a-col>
                      <a-col :span="24">
                        <a-form-model-item label="权重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weight">
                          <a-input-number v-model="model.weight" placeholder="请输入权重" style="width: 100%" />
                        </a-form-model-item>
                      </a-col>
                      <a-col :span="24">
                        <a-form-model-item
                          label="评审说明"
                          :labelCol="labelCol"
                          :wrapperCol="wrapperCol"
                          prop="description"
                        >
                          <a-textarea v-model="model.description" rows="4" placeholder="请输入评审说明" />
                        </a-form-model-item>
                      </a-col>
                    </a-row>
                  </a-form-model>
                </j-form-container>
              </a-spin>
            </a-row>
          </a-card>
        </a-col>
      </a-row>
    </a-card>

    <!-- <footer-tool-bar>
      <a-button type="primary" @click="handleSubmit" :loading="loading">提交</a-button>
      <a-button style="margin-left: 8px" @click="handleSave">保存</a-button>
    </footer-tool-bar> -->
  </a-spin>
</template>

<script>
import FooterToolBar from '@/components/tools/FooterToolBar'
import '@/assets/less/TableExpand.less'
import ChapterList from '@/views/exZbwj/ScoreChapter/ChapterListMini'

import { httpAction, getAction } from '@/api/manage'

export default {
  name: 'exTenderFileAdd',
  components: { ChapterList, FooterToolBar },
  props: {
    entryId: {
      default: '',
    },
    standardType: {
      //指标类型（评分指标、价格指标
      default: '',
    },
  },
  data() {
    return {
      disableSubmit: false,
      visible: false,
      varName: null,
      noticeContent: '',
      confirmLoading: false,
      scoreStandardId: '',
      scoreStandardInfo: { projectId: '', id: '' },
      model: {},
      validatorRules: {
        indicatorName: [{ required: true, message: '请输入指标名称!', }],
        indicatorType: [{ required: true, message: '请选择指标类型!' }],
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 10 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 12 },
      },
    }
  },
  watch: {
    entryId: {
      immediate: true,
      handler(val) {
        if (val != '' && val != undefined) {
          this.scoreStandardId = val
          this.loadScoreStandardInfo()
        }
      },
    },
  },
  created() {
    this.scoreStandardId = this.$route.query.scoreStandardId
    if (this.scoreStandardId) {
      this.loadScoreStandardInfo()
    } else {
    }
  },
  mounted() {
    //this.$refs.realForm.addBefore()
  },
  methods: {
    handleCancel() {
      this.close()
    },
    loadScoreStandardInfo() {
      var that = this
      getAction('exScoreStandard/exScoreStandard/queryById?id=' + that.scoreStandardId)
        .then((res) => {
          if (res.success) {
            that.scoreStandardInfo = res.result
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.confirmLoading = false
        })
    },
    handleSubmit() {
      this.handleSave()
    },
    async handleSave() {
      var that = this
      if (that.$refs.chapterRef.selectedRowKeys.length == 0) {
        that.$message.warning('请选择分类')
        return
      }
      return new Promise((resolve,reject)=>{
        this.$refs.form.validate((valid) => {
          if (valid) {
            var cid = that.$refs.chapterRef.selectedRowKeys[0]
            var url = 'exTenderFile/exZbwjScoreStandardDetail/add'
            let param = Object.assign({}, that.model)
            param.categoryId = cid
            if (that.model && that.model.id) {
              param.id = that.model.id
              url = 'exTenderFile/exZbwjScoreStandardDetail/edit'
            }
            param.standardType = this.standardType
            httpAction(url, param, 'post')
              .then((res) => {
                if (res.success) {
                  that.$message.success(res.message)
                } else {
                  that.$message.warning(res.message)
                }
                resolve();
              })
              .finally(() => {
                that.confirmLoading = false
              })
          }else{
            reject();
          }
        })
      })

    },
    async chapterChange(selectVal,oldSelect) {
      if(oldSelect?.length){
        this.handleSave().catch(()=>{
          throw new Error('保存失败!');
        })
      }
      var that = this
      that.confirmLoading = true
      getAction('exTenderFile/exZbwjScoreStandardDetail/list', { categoryId: selectVal })
        .then((res) => {
          if (res.success) {
            if (res.result.total > 0) {
              that.model = res.result.records[0]
            } else {
              that.model = {}
            }
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.confirmLoading = false
        })
    },
    exTenderFileInfoEdit() {
      this.$refs.refSetVariableList.show()
    },
  },
}
</script>

<style scoped>
</style>
